import { ElMessage, ElNotification, ElMessageBox } from 'element-plus'

import taskFactory from '@/utils/task'
import { type LoginUser } from '@/server'

// import { type uploadImageType } from '@/apis'
import { baseApi } from '@/server/base'
const { pushLoopTask, pushLoopTaskList, executeLoopTask, clearTask } = taskFactory()

export default {
  props: {
    refreshTabPage: {
      type: Function,
      default: () => {
        return function () { }
      }
    },
    key: String,
    activePageName: String,


  } as any,
  data() {
    return {

      showTeleport: false,
      // 用一串无意义的字符标识 true和false，避免路由传递  true/false 1/0被修改路由访问
      urlPathCode: {
        'TRUE': '1030301820052',
        'FALSE': '0023031800242'
      } as { TRUE: string, FALSE: string }
    }
  },

  emits: ['showDialogPageHandler'],

  computed: {
    /**
     *  普通页面内容高度
     *  适用于单行 header
     */
    normalBodyHeight() {
      return {
        height: 'calc(100% - 40px)',
        overflow: 'auto'
      }
    },
    /**
     * 用户信息
     */
    userInfo(): LoginUser {
      return (this as any).$store.getters.userInfo
    },
    userName(): string {
      return (this.userInfo as any).username || (this.userInfo as any).jobNumber
    },
    account(): string {
      return (this.userInfo as any).jobNumber
    },


    id(): string | number {
      return (this as any).$route.params?.id;
    },
    isEdit(): boolean | undefined {
      const isEdit = (this as any).$route.params?.isEdit;
      if (!isEdit) {
        return undefined;
      }
      return (this as any).urlPathCode.TRUE == isEdit;
    }
  },
  methods: {
    /**
    * 打开弹窗
    * @param route 
    */
    dialogPagePush(route: any) {
      (this as any).$emit('showDialogPageHandler', { route, isShowDialogPage: true });
    },
    /**
    * 获取 对象 转成字符串
    * @param item
    * @returns
    */
    getParamsString(item: any | null) {
      if (!item) {
        return ''
      }
      return encodeURI(JSON.stringify(item))
    },
    /**
     * 获取字符串转成对象
     * @param itemStr
     * @returns
     */
    getParamsByString(itemStr: string | null) {
      if (!itemStr) {
        return null;
      }
      return JSON.parse(decodeURI(itemStr))
    },

    getUrlBooleanValue(value: string) {
      const that = this as any;
      if (value == that.urlPathCode.TRUE) {
        return true;
      }
      return false;
    },
    /**
     * 获取图片路径
     * @param img
     */

    getImageFilePath(img: any): string {

      return `${baseApi}/${img.disk}/${img.path}`
    },
    /**
     *定时任务
     */
    pushLoopTask,
    pushLoopTaskList,
    executeLoopTask,
    clearTask,
    /**
     * 前进或后退
     * @param go 负值后退，正值前进 0时刷新
     * @param reload 是否刷新
     * @returns true:可以后退  false:不可后退，此时可以通过返回值做其他处理
     */
    goBackOrForward(go: number = -1, reload: boolean = false) {
      if (go === 0) {
        window.location.reload()
        return true;
      }
      const position = window.history.state.position + go

      if (position > window.history.length) {

        // ElMessage({ type: 'error', message: '已经到达最顶层！' })
        (this as any).$router.push('/layout')
        return false;
      }
      if (position < 0) {
        ElMessage({ type: 'error', message: '已经退到最低层！' })
        return false;
      }
      window.history.go(go)
      if (reload) {
        window.location.reload()
      }
      return true;
    },

    setFrameLoading(loading = true) {
      //設置加載動畫時常
      ; (this as any).$emit('frameCallback', { name: 'FrameLoading', arguments: loading })
    },

    showNotify(options: any) {
      ElNotification(options)
    },

    showMessage(option: any) {
      ElMessage(option);
    },

    $confirm: (value: string, title: string = '提示') => ElMessageBox.confirm(value, title,{}),


    getRenderTableValue(layout: any, row: any) {
      const { prop, format } = layout

      if (typeof format === 'undefined' && !row[prop]) {
        return ''
      }
      if (typeof format === 'function') {
        return format(row[prop], row)
      }
      return row[prop]
    },

    /**
     * 获取路由信息
     * @param name 
     * @returns 
     */
    getRouteByName(name: string) {
      const routes = (this as any).$router.options.routes as any[]
      return this.getRoute(routes, name)
    },
    /**
     * 递归查找路由信息
     * @param routes 
     * @param name 
     * @returns 
     */
    getRoute(routes: any[], name: string): object {
      const _route = routes.filter(item => item.name === name)
      if (_route && _route.length > 0) {
        return _route[0]
      }
      const routeChildren = routes.filter(item => item.children).map(item => item.children)
      for (let i = 0; i < routeChildren.length; i++) {
        const _route2 = this.getRoute(routeChildren[i], name)
        if (_route2 && Object.keys(_route2).length > 0) {
          return _route2
        }
      }
      return {}
    },

  },
  mounted() {
    (this as any).showTeleport = true;
  },
  created() {
    //設置加載動畫時常
    ; (this as any).$emit('frameCallback', { name: 'frameLoadingTime', arguments: 300 })
  }
}
